import os,sys
from subprocess import Popen

#p = Popen("mycmd" + " myarg", shell=True)
def encode(szsrc,szdst):
    
    szdir = os.path.dirname(szdst)
    try:
        os.makedirs(szdir)
    except:
        pass
    bin_hb = 'HandBrakeCLI'
    #szexec = 'HandBrakeCLI -i %(szsrc)s -o %(szdst)s --preset="iPhone"' % locals()
    szexec = '''%(bin_hb)s  -e x264  -q 20.0 -E faac -B 128 -6 dpl2 -R Auto -D 0.0 -f mp4 -X 480 -m  \
    -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0 --optimize  \
    --input %(szsrc)s --output %(szdst)s --format mp4'''%locals()        
    print szexec
    
    if sys.platform!='win32':
        p = Popen(szexec, shell=True)
        sts = os.waitpid(p.pid, 0)[1]
    else:
        print '@WIN32, JUST PRINT EXEC'
    print 'DONE'
    
if __name__ == '__main__':    
    szsrc = '/t/in/clock.avi'
    szdst = '/t/out/clock.mp4'    
    #szsrc = '/t/we.avi'
    #szdst = '/t/we.mp4'        
    encode(szsrc,szdst)